#include "add_d_ch.h"
#include "ui_add_d_ch.h"
#include <QDebug>
#include <QMessageBox>

ADD_D_CH::ADD_D_CH(QString str,QString sfreq,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ADD_D_CH)
{
    ui->setupUi(this);
    switch_language(str);
    language = str;
    freq = sfreq;
    init(str);

    connect(ui->power,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox(QString)));
    connect(ui->colorcode,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_colorcode(QString)));
    connect(ui->mode,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_mode(QString)));
    connect(ui->call,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_call(QString)));
    connect(ui->output,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_outputtime(QString)));
    connect(ui->input,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_inputtime(QString)));
    //connect(ui->volume,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_volume(QString)));
    //connect(ui->gain,SIGNAL(currentIndexChanged(QString)),this,SLOT(deal_combox_gain(QString)));

    //密钥默认为空
    ui->key->setText("");
    //呼叫列表
     slist = "1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0";
    //ui->list->setText(slist);

    ui->colorcode->setCurrentIndex(1);
    //ui->volume->setCurrentIndex(5);
    //ui->gain->setCurrentIndex(4);

    name = ui->name->text();
    recvfreq = ui->recvfreq->text();
    sendfreq = ui->sendfreq->text();
    id = ui->id->text();
    power = ui->power->currentText();
    colorcode = ui->colorcode->currentText();
    mode = ui->mode->currentText();
    call = ui->call->currentText();
    output = ui->output->currentText();
    input = ui->input->currentText();
    //volume = ui->volume->currentText();
    //gain = ui->gain->currentText();
    volume = "0";
    gain = "0";
    num = ui->num->text();
    //list = ui->list->text();
    list = slist;
    key = ui->key->text();
    lencrypt = "0";

    qDebug() << "初始值" << name << " "<< recvfreq << " "<< sendfreq << " "<< id << " "
             << power << " "<< colorcode << " "<< mode<< " "
             << call << " "<< output << " "<< input << " "<< volume << " " <<gain
             << num << list << key << lencrypt;
}

ADD_D_CH::~ADD_D_CH()
{
    delete ui;
}

void ADD_D_CH::init(QString language)
{
    if (language == "Chinese")
    {
        ui->power->insertItem(0,"高","111");
        ui->power->insertItem(1,"低","111");
        ui->power->setCurrentIndex(0);

        ui->mode->insertItem(0,"直通模式","111");
        ui->mode->insertItem(1,"真双时隙模式","111");
        ui->mode->setCurrentIndex(0);

        ui->call->insertItem(0,"个呼","111");
        ui->call->insertItem(1,"组呼","111");
        ui->call->insertItem(2,"全呼","111");
        ui->call->setCurrentIndex(1);

        ui->output->insertItem(0,"时隙1","111");
        ui->output->insertItem(1,"时隙2","111");
        ui->output->setCurrentIndex(0);

        ui->input->insertItem(0,"时隙1","111");
        ui->input->insertItem(1,"时隙2","111");
        ui->input->setCurrentIndex(0);

        ui->encrypt->insertItem(0,"关","111");
        ui->encrypt->insertItem(1,"开","111");
        ui->encrypt->setCurrentIndex(0);

    }

    if (language == "English")
    {
        ui->power->insertItem(0,"High","111");
        ui->power->insertItem(1,"Low","111");
        ui->power->setCurrentIndex(0);

        //ui->mode->insertItem(0,"Pass-through","111");
        ui->mode->insertItem(0,"Direct-Mode","111");
        ui->mode->insertItem(1,"Double-timeslot","111");
        ui->mode->setCurrentIndex(0);

//        ui->call->insertItem(0,"A-call","111");
//        ui->call->insertItem(1,"Group-call","111");
//        ui->call->insertItem(2,"All-call","111");
        ui->call->insertItem(0,"Private","111");
        ui->call->insertItem(1,"Group","111");
        ui->call->insertItem(2,"All","111");
        ui->call->setCurrentIndex(1);

//        ui->output->insertItem(0,"time-slot1","111");
//        ui->output->insertItem(1,"time-slot2","111");
        ui->output->insertItem(0,"Slot1","111");
        ui->output->insertItem(1,"Slot2","111");
        ui->output->setCurrentIndex(0);

        ui->input->insertItem(0,"Slot1","111");
        ui->input->insertItem(1,"Slot2","111");
        ui->input->setCurrentIndex(0);

        ui->encrypt->insertItem(0,"Close","111");
        ui->encrypt->insertItem(1,"Open","111");
        ui->encrypt->setCurrentIndex(0);
    }
    ui->recvfreq->setText(freq);
    ui->sendfreq->setText(freq);
}

void ADD_D_CH::deal_combox(QString str)
{
    QString power = ui->power->currentText();
    qDebug() << "功率为" << power;
}

void ADD_D_CH::deal_combox_colorcode(QString str)
{
    QString  colorcode = ui->colorcode->currentText();
    qDebug() << "色码为" << colorcode;
}

void ADD_D_CH::deal_combox_encrypt(QString str)
{
//    QString  colorcode = ui->colorcode->currentText();
//    qDebug() << "色码为" << colorcode;
}

void ADD_D_CH::deal_combox_mode(QString str)
{
    QString  colorcode = ui->mode->currentText();
    qDebug() << "模式为" << colorcode;
}

void ADD_D_CH::deal_combox_call(QString str)
{
    QString  colorcode = ui->call->currentText();
    qDebug() << "呼叫类型" << colorcode;
}

void ADD_D_CH::deal_combox_outputtime(QString str)
{
    QString  colorcode = ui->output->currentText();
    qDebug() << "输出时隙" << colorcode;
}

void ADD_D_CH::deal_combox_inputtime(QString str)
{
    QString  colorcode = ui->input->currentText();
    qDebug() << "输入时隙" << colorcode;
}

void ADD_D_CH::deal_combox_volume(QString str)
{
    //QString  colorcode = ui->volume->currentText();
    //qDebug() << "音量" << colorcode;
}
void ADD_D_CH::deal_combox_gain(QString str)
{
    //QString  colorcode = ui->gain->currentText();
   // qDebug() << "增益" << colorcode;
}

void ADD_D_CH::on_pushButton_clicked()
{
    if (ui->name->text().isEmpty() ||
        ui->recvfreq->text().isEmpty() ||
        ui->sendfreq->text().isEmpty() ||
        ui->id->text().isEmpty()) {
        if (language == "English")
            QMessageBox::warning(NULL, "prompt", "Please improve the data", "OK");
        if (language == "Chinese")
            QMessageBox::warning(NULL, "提示", "请完善数据", "确定");

        return;
    }

    if (ui->num->text().size() > 6) {
        if (language == "English")
            QMessageBox::warning(NULL, "prompt", "[Contact Num]The input length cannot exceed 6 characters", "OK");
        if (language == "Chinese")
            QMessageBox::warning(NULL, "提示", "[Contact Num]输入内容不能超过6个字符", "确定");
        return;
    }

    if (ui->key->text().size() > 6) {
        if (language == "English")
            QMessageBox::warning(NULL, "prompt", "[Encrypt Key]The input length cannot exceed 6 characters", "OK");
        if (language == "Chinese")
            QMessageBox::warning(NULL, "提示", "[Encrypt Key]输入内容不能超过6个字符", "确定");
        return;
    }

    if (ui->sendfreq->text().size() > 10) {
        if (language == "English")
            QMessageBox::warning(NULL, "prompt", "[TX Freq]The input length cannot exceed 9 characters", "OK");
        if (language == "Chinese")
            QMessageBox::warning(NULL, "提示", "[TX Freq]输入内容不能超过9个字符", "确定");
        return;
    }

    if (ui->recvfreq->text().size() > 10) {
        if (language == "English")
            QMessageBox::warning(NULL, "prompt", "[RF Freq]The input length cannot exceed 9 characters", "OK");
        if (language == "Chinese")
            QMessageBox::warning(NULL, "提示", "[RF Freq]输入内容不能超过9个字符", "确定");
        return;
    }

    name = ui->name->text();
    recvfreq = ui->recvfreq->text();
    sendfreq = ui->sendfreq->text();
    id = ui->id->text();
    power = ui->power->currentText();
    colorcode = ui->colorcode->currentText();
    mode = ui->mode->currentText();
    call = ui->call->currentText();
    output = ui->output->currentText();
    input = ui->input->currentText();
    //volume = ui->volume->currentText();
    //gain = ui->gain->currentText();
    volume = "0";
    gain = "0";

    num = ui->num->text();
    //list = ui->list->text();
    list = slist;
    key = ui->key->text();
    lencrypt = ui->encrypt->currentText();

    QString str = QString("%1#%2#%3#%4#%5#%6#%7#%8#%9#%10#%11#%12#%13#%14#%15#%16").arg(name).arg(recvfreq).arg(sendfreq).arg(id)
            .arg(power).arg(colorcode).arg(mode).arg(call).arg(output).arg(input).arg(volume).arg(gain)
            .arg(num).arg(list).arg(key).arg(lencrypt);
    emit send_str(str);
    this->close();
}

void ADD_D_CH::switch_language(QString language)
{
    if (language == "Chinese") {
        ui->lname->setText("信道名称");
        ui->lrecvfreq->setText("接收频率");
        ui->lsendfreq->setText("发射频率");
        ui->lid->setText("本地ID");
        ui->lpower->setText("功率");
        ui->lcolorcode->setText("色码");
        ui->lmode->setText("信道模式");
        ui->lcall->setText("呼叫类型");
        ui->loutput->setText("输出时隙");
        ui->linput->setText("输入时隙");
        //ui->lvolume->setText("音量");
        //ui->lgain->setText("增益");
        ui->lnum->setText("联系人号码");
        ui->llist->setText("组呼列表");
        ui->lkey->setText("密钥");
        ui->lencrypt->setText("加密");
        ui->pushButton->setText("确定");
        ui->pushButton_2->setText("点击修改");
    }

    if (language == "English") {
        ui->lname->setText("Channel Name");
        ui->lrecvfreq->setText("RF Freq");
        ui->lsendfreq->setText("TX Freq");
        ui->lid->setText("Local ID");
        ui->lpower->setText("Power");
        ui->lcolorcode->setText("Color Code");
        ui->lmode->setText("Channel Mode");
        ui->lcall->setText("Contact Mode");
        ui->loutput->setText("Output Slot");
        ui->linput->setText("Input Slot");
        //ui->lvolume->setText("Volume");
        //ui->lgain->setText("Gain");
        ui->lnum->setText("Contact Num");
        ui->llist->setText("Group List");
        ui->lkey->setText("Encrypt Key");
        ui->lencrypt->setText("Encrypt");
        ui->pushButton->setText("Enter");
        ui->pushButton_2->setText("Click Edit");
    }
}

void ADD_D_CH::deal_group_list(QString str)
{
    slist = str;
}

void ADD_D_CH::on_pushButton_2_clicked()
{
    grouplist *glist = new grouplist(slist);
    connect(glist,SIGNAL(send_str(QString)),this,SLOT(deal_group_list(QString)));
    glist->exec();
}

void ADD_D_CH::setFreq(QString str)
{
    freq = str;
}
